package com.commerce.reader.utils;



import android.util.Log;

import com.commerce.reader.Entity.Book;
import com.commerce.reader.Entity.Books;
import com.commerce.reader.Entity.Chapter;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.litepal.LitePal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public  class JsoupHtml {

    private static final String TAG = "JsoupHtml";
    private static String ad="亲,点击进去,给个好评呗,分数越高更新越快,据说给新笔趣阁打满分的最后都找到了漂亮的老婆哦! 手机站全新改版升级地址：http://m.xbiquge.la，数据和书签与电脑站同步，无广告清新阅读！";

    /**
     * 解析html图书的链接
     * @param url  书库链接
     * @return 书库
     */
    public static void toBooksUrl(String url)  {
        Document document = connectURL(url);
        if(document!=null){
            LitePal.deleteAll(Books.class);
        }
        Elements as = document.getElementsByTag("a");//获取所有的a标签的元素
        Books books= null;
        for (Element element:as) {//遍历a标签
            books = new Books();
            String href = element.attr("href");//获取链接的url值
            String text = element.text();//获取链接的标题
            //筛选链接
            if(href.startsWith("http://www.xbiquge.la/")&&!href.equals("http://www.xbiquge.la/")){
                books.setName(text);
                books.setUrl(href);
                books.save();
            }
        }
    }

    /**
     *  解析图书的章节
     * @param url 图书链接
     * @return 章节键值对
     */
    public static  Book toBookUrl(String url)  {
        Book book = new Book();
        Document document = connectURL(url);
        Elements asa = document.getElementsByTag("a");//获取所有的a标签的元素
        Elements asp = document.getElementsByTag("p");
        Elements asimg = document.getElementsByTag("img");
        book.setName(asp.get(0).text());
        book.setTime(asp.get(2).text());
        book.setDesc(asp.get(5).text());
        book.setImgurl(asimg.get(1).attr("src"));
        book.setBook(asimg.get(1).attr("alt"));
        List<Chapter> map=new ArrayList<>();
        for (Element element:asa) {//遍历a标签
            Chapter chapter = new Chapter();
            String href = element.attr("href");//获取链接的url值
            String text = element.text();//获取链接的标题
            //筛选链接
            if(href.startsWith(url.replace("http://www.xbiquge.la",""))){
                chapter.setIdName(asimg.get(1).attr("alt")+asp.get(0).text());
                chapter.setChapter(text);
                chapter.setUrl("http://www.xbiquge.la"+href);
                map.add(chapter);
            }
        }
        book.setMap(map);
        return book;
    }
    /**
     * 解析章节内容
     * @param url     章节连接
     * @return 内容
     */
    public static String toBook(String url)  {
        Document document = connectURL(url);
        Elements as = document.getElementsByTag("div");  //获取所有的p标签的元素
        String text = as.get(10).text().replace(ad,""); //去广告
        return text;
    }

    /**
     * 获取网页内容
     * @param url   链接
     * @return   存放内容的documnet类
     */
    public static Document connectURL(String url)  {
        Document document = null;
            while (document == null) {
                try {
                    document = Jsoup.connect(url).timeout(5*1000).get();  //获取连接，选择发送方式,获取整个网页信息，存在documnet类里
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        return document;
    }
}
